package com.yyw.mcar.base;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.PersistableBundle;
import android.support.v4.app.FragmentActivity;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.yyw.mcar.R;

import de.greenrobot.event.EventBus;

/**
 * @Title: 基础的activity类
 * @Description: 所有activity的基类
 * @Author: wanghaixiao
 * @Date: 2016/9/21
 * @Version:1.0.2
 */
public class BaseActivity extends FragmentActivity {

    public Context context;

    // 退出的时间定义
    long lastPressedTime = 0;
    long exitTime = 1000;// 两次下按的时间间隔

    private boolean isResume = false;

    public static final int SHOWDIALOG = 1;
    public static final int HIDEDIALOG = 2;

    public ProgressDialog pd;

    @Override
    public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
            if (Build.VERSION.SDK_INT < 16) {
                getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                        WindowManager.LayoutParams.FLAG_FULLSCREEN);
            }  else {
                View decorView = getWindow().getDecorView();
                // Hide the status bar.
                int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
                decorView.setSystemUiVisibility(uiOptions);
                // Remember that you should never show the action bar if the
                // status bar is hidden, so hide that too if necessary.
//	        	ActionBar actionBar = getActionBar();
//	        	actionBar.hide();
            }

        EventBus.getDefault().register(this);
        context = this;

        /**
         * 初始对话框
         */
        pd = new ProgressDialog(context);
        pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        pd.setCancelable(false);
        pd.setOnKeyListener(onKeyListener);
    }

    protected void fillStatusBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window window = getWindow();
            // Translucent status bar
            window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            // Translucent navigation bar
            window.setFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
    }

    protected void setTransLucentStatus() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window window = getWindow();
            window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.setFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
    }

    private DialogInterface.OnKeyListener onKeyListener = new DialogInterface.OnKeyListener() {
        @Override
        public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_BACK
                    && event.getAction() == KeyEvent.ACTION_DOWN) {
                dismissDialog();
            }
            return false;
        }
    };

    /**
     * 关闭对话框
     *
     * @author 王海啸
     */
    public void dismissDialog() {
        if (isFinishing()) {
            return;
        }
        if (null != pd && pd.isShowing()) {
            pd.dismiss();
        }
    }

    public Handler baseHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case SHOWDIALOG:
                    if (pd != null) {
                        if (!isFinishing()) {
                            pd.show();
                        }
                    }
                    break;
                case HIDEDIALOG:
                    if (pd != null) {
                        pd.hide();
                    }
                    break;
                default:
                    break;
            }
        }

    };

    /**
     * 显示对话框
     */
    public void showDialog() {
        baseHandler.sendEmptyMessage(SHOWDIALOG);
    }

    /**
     * 隐藏对话框
     */
    public void hideDialog() {
        baseHandler.sendEmptyMessage(HIDEDIALOG);
    }

    @Override
    protected void onResume() {
        super.onResume();
        isResume = true;
    }

    public void onEventMainThread() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);// 反注册EventBus
    }


   /* * 退出应用程序
    */
    public void AppExit(Context context) {
        try {
            ActivityManager activityMgr= (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            activityMgr.restartPackage(context.getPackageName());
            System.exit(0);
        } catch (Exception e) { }
    }

    /**
     * 关闭指定的Activity
     *
     * @param activity
     */
    public void finishActivity(FragmentActivity activity) {
       BaseApplication.getInstance().finishActivity(activity);
        overridePendingTransition(R.anim.activity_left_harf_in,
                R.anim.activity_right_out);
    }

    /**
     * 仅关闭无动画
     *
     * @param activity
     */
    public void onlyExit(FragmentActivity activity) {
        BaseApplication.getInstance().finishActivity(activity);
    }

    /**
     * 关闭当前activity
     */
    public void exitThisOnly() {
        finishActivity(this);
    }

    // 退出
    public void exitThis() {
        finishActivity(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        isResume = false;
    }
}
